package com.chunxuan.langquan.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chunxuan.langquan.R;
import com.chunxuan.langquan.base.Base2Activity;
import com.chunxuan.langquan.base.BaseResult2;
import com.chunxuan.langquan.base.Global;
import com.chunxuan.langquan.dao.bean.PageBean;
import com.chunxuan.langquan.dao.bean.SubjectMore;
import com.chunxuan.langquan.dao.http.APIRetrofit2;
import com.ruitu.arad.api.RxUtil;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;
import com.ruitu.arad.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class SubjectListMoreActivity extends Base2Activity {
    private ImageView iv_back;
    private TextView tv_title,tv_subject_title;
    private RecyclerView rcv_list;// 科目列表

    private String grade_category_id, subject_category_id, class_category_id, course_id;// 传过来的id
    private String title;// 传过来的标题

    private SubjectListAdapter subjectAdapter;
    private List<SubjectMore> subjectMoreList = new ArrayList<>();

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_subject_list_more;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        iv_back = findViewById(R.id.iv_back);
        tv_title = findViewById(R.id.tv_title);
        tv_subject_title = findViewById(R.id.tv_subject_title);
        rcv_list = findViewById(R.id.rcv_list);

        grade_category_id = getIntent().getStringExtra("grade_category_id");
        subject_category_id = getIntent().getStringExtra("subject_category_id");
        class_category_id = getIntent().getStringExtra("class_category_id");
        course_id = getIntent().getStringExtra("course_id");
        title = getIntent().getStringExtra("title");

        tv_title.setText(title);
        tv_subject_title.setText(title);

        GridLayoutManager manager = new GridLayoutManager(this, 2);
        rcv_list.setLayoutManager(manager);
        subjectAdapter = new SubjectListAdapter(this);
        rcv_list.setAdapter(subjectAdapter);

        setOnClickListener(iv_back);

        reqSubjectMore();
        showProgress();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == iv_back) {
            onBackPressed();
        }
    }

    private Disposable reqSubjectMore() {
        return APIRetrofit2.getDefault().reqSubjectMore(Global.HEADER, subject_category_id, class_category_id, "1", "999999")
                .compose(RxUtil.subIoObMain())
                .subscribe(new Consumer<BaseResult2<PageBean<SubjectMore>>>() {
                    @Override
                    public void accept(BaseResult2<PageBean<SubjectMore>> pageResult2) throws Exception {
                        hideProgress();
                        if (pageResult2.isSuccess()) {
                            PageBean<SubjectMore> pageBean = pageResult2.getData();
                            subjectMoreList = pageBean.getData();
                            subjectAdapter.setDataList(subjectMoreList);
                        } else {
                            ToastUtils.showShort(pageResult2.getMsg());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        hideProgress();
                        throwable.printStackTrace();
                        ToastUtils.showShort("获取课程信息错误");
                    }
                });
    }

    private class SubjectListAdapter extends ListBaseAdapter<SubjectMore> {

        public SubjectListAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_subject_more_list;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            RelativeLayout rl_item = holder.getView(R.id.rl_item);
            TextView tv_course_title = holder.getView(R.id.tv_course_title);
            TextView tv_subject_summary = holder.getView(R.id.tv_subject_summary);

            SubjectMore subjectMore = mDataList.get(position);
            if(subjectMore.getTitle().toString().length()>13){
                tv_course_title.setText("     "+subjectMore.getTitle());
            }else{
                tv_course_title.setText(subjectMore.getTitle());
            }

            if(subjectMore.getSummary().toString().length()>15){
                tv_subject_summary.setText("     "+subjectMore.getSummary());
            }else{
                tv_subject_summary.setText(subjectMore.getSummary());
            }

            rl_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(SubjectListMoreActivity.this, CourseDetailActivity.class);
                    intent.putExtra("grade_category_id", subjectMore.getGrade_category_id());
                    intent.putExtra("subject_category_id", subjectMore.getSubject_category_id());
                    intent.putExtra("class_category_id", subjectMore.getClass_category_id());
                    intent.putExtra("course_id", subjectMore.getId());
                    startActivity(intent);
                }
            });
        }
    }
}